import sar.Requestmap
import sar.Role
import sar.User
import sar.UserRole;

class BootStrap {
	def springSecurityService
	def init = {
		/**
		 * Creare User and Admin role
		 */
		def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)
		def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
		def studentRole = Role.findByAuthority('ROLE_STUDENT') ?: new Role(authority: 'ROLE_STUDENT').save(failOnError: true)
		def studentTutor = Role.findByAuthority('ROLE_TUTOR') ?: new Role(authority: 'ROLE_TUTOR').save(failOnError: true)
		def studentStaff = Role.findByAuthority('ROLE_STAFF') ?: new Role(authority: 'ROLE_STAFF').save(failOnError: true)
		def studentCourseCoordinator= Role.findByAuthority('ROLE_COURSECOORDINATOR') ?: new Role(authority: 'ROLE_COURSECOORDINATOR').save(failOnError: true)
		/**
		 * Create Admin
		 */
		def adminUser = User.findByUsername('admin') ?: new User(
				username: 'admin',
				password: 'admin',
				enabled: true).save(failOnError: true)

		if (!adminUser.authorities.contains(adminRole)) {
			new UserRole(user: adminUser, role:adminRole).save(flush:true)
		}

		/**
		 * Create mapping URL
		 */
		if(Requestmap.count==0){
			for (String url in [
				'/',
				'/index',
				'/index.gsp',
				'/**/favicon.ico',
				'/**/js/**',
				'/**/css/**',
				'/**/images/**',
				'/login',
				'/login.*',
				'/login/*',
				'/logout',
				'/logout.*',
				'/logout/*'
			]) {
				new Requestmap(url: url, configAttribute: 'permitAll').save()
			}
			/*new Requestmap(url: '/profile/**',    configAttribute: 'ROLE_USER').save()
			 new Requestmap(url: '/**',      configAttribute: 'ROLE_ADMIN').save()*/
			/*Program & Course Management*/
			new Requestmap(url: '/user/**', configAttribute: 'ROLE_ADMIN').save()
			new Requestmap(url: '/program/**', configAttribute: 'ROLE_ADMIN').save()
			new Requestmap(url: '/course/**', configAttribute: 'ROLE_ADMIN').save()
			new Requestmap(url: '/programCourse/**', configAttribute: 'ROLE_ADMIN').save()

			/*Security Management*/
			new Requestmap(url: '/role/**', configAttribute: 'ROLE_ADMIN').save()
			new Requestmap(url: '/requestmap/**', configAttribute: 'ROLE_ADMIN').save()

			/*Content Management*/
			new Requestmap(url: '/teachingSession/**', configAttribute: 'ROLE_COURSECOORDINATOR,ROLE_ADMIN').save()
			new Requestmap(url: '/place/**', configAttribute: 'ROLE_COURSECOORDINATOR,ROLE_ADMIN').save()
			new Requestmap(url: '/sessionPlace/**', configAttribute: 'ROLE_COURSECOORDINATOR,ROLE_ADMIN').save()

			/*Student & Course*/
			new Requestmap(url: '/student/**', configAttribute: 'ROLE_COURSECOORDINATOR,ROLE_ADMIN').save()
			new Requestmap(url: '/studentCourse/**', configAttribute: 'ROLE_COURSECOORDINATOR,ROLE_ADMIN').save()

			/*Attendant Student*/
			new Requestmap(url: '/studentAttendant/**', configAttribute: 'ROLE_ADMIN,ROLE_TUTOR').save()

			/*User management*/
			new Requestmap(url: '/staff/**', configAttribute: 'ROLE_ADMIN').save()
			new Requestmap(url: '/tutor/**', configAttribute: 'ROLE_ADMIN').save()
			new Requestmap(url: '/courseCoordinator/**', configAttribute: 'ROLE_ADMIN').save()
			/*Report Management*/
			new Requestmap(url: '/teachingReport/**', configAttribute: 'ROLE_ADMIN,ROLE_TUTOR,ROLE_STAFF,ROLE_STUDENT').save()
			new Requestmap(url: '/studentAttendentReport/**', configAttribute: 'ROLE_ADMIN,ROLE_TUTOR,ROLE_STAFF').save()


			new Requestmap(url: '/j_spring_security_switch_user',
			configAttribute: 'ROLE_SWITCH_USER,isFullyAuthenticated()').save()
		}
	}
	def destroy = {
	}
}
